﻿namespace Web.Control.WWW.Answers
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.IO;
    using Lx.Web.Common.ControlsUI;
    using Lx.Web.Common.UI;
    using Model.WWW_WWW.Answers;
    using BLF.WWW_WWW.Answer;
    using System.Collections.Generic;
    using BLF;

    public class Search : BaseControl
    {
        /// <summary>
        /// 显示的类型（1为Most Recent ，2为Most Views ，3为No. of Answers ）
        /// </summary>
        protected int sort = 1;
        /// <summary>
        /// 页面
        /// </summary>
        protected int pageIndex = 1;
        /// <summary>
        /// 目标语言
        /// </summary>
        protected string Learn = "";
        /// <summary>
        /// 使用语言
        /// </summary>
        protected string BaseL = "";
        /// <summary>
        /// 关键字
        /// </summary>
        protected string key = "";
        /// <summary>
        /// 问题的类型1.All questions；2.Open questions；3.Undecided questions；4.None Answers；5.Resolved questions；
        /// </summary>
        protected int st = 1;
        protected Repeater repQuest;
        protected Repeater repTags;
        protected Repeater rptLL;
        /// <summary>
        /// 数据数量
        /// </summary>
        protected int RecordCount = 0;
        protected string sortField = "views";
        protected List<AnswersTagModel> anList;
        protected List<QuestionModel> quList;
        protected string show="1";
        protected override void OnLoad(EventArgs e)
        {
            if (Request.Cookies["italkiLearn"] == null)
            {
                HttpCookie llCookie = new HttpCookie("italkiLearn", "");
                Response.Cookies.Add(llCookie);
            }
            if (Request.Cookies["italkiSpeak"] == null)
            {
                HttpCookie blCookie = new HttpCookie("italkiSpeak", "");
                Response.Cookies.Add(blCookie);
            }
            if (!string.IsNullOrEmpty(Request["ll"]) && Request["ll"] != "")
            {
                Learn = Request["ll"];
                HttpCookie llCookie = new HttpCookie("italkiLearn", Learn);
                Response.Cookies.Add(llCookie);
            }
            else
            {
                Learn = Request.Cookies["italkiLearn"].Value;
            }
            if (!string.IsNullOrEmpty(Request["bl"]) && Request["bl"] != "")
            {
                BaseL = Request["bl"];
                HttpCookie blCookie = new HttpCookie("italkiSpeak", BaseL);
                Response.Cookies.Add(blCookie);
            }
            else
            {
                BaseL = Request.Cookies["italkiSpeak"].Value;
            }
            if (!string.IsNullOrEmpty(Request["st"]))
            {
                st = int.Parse(Request["st"].ToString());
            }
            if (!string.IsNullOrEmpty(Request["key"]))
            {
                key = Request["key"].ToString();
            }
            if (!string.IsNullOrEmpty(Request["sort"]))
            {
                sort = int.Parse(Request["sort"].ToString());
            }
            if (key.Trim() == "" || key.Trim().Length >= 3)
            {
                if (key.Trim() != "" || sort != 1 || BaseL != "" || Learn != "" || st != 1)
                {
                    show = "0";

                    
                   
                    if (sort == 3)
                    {
                        sortField = "AnswerCount";
                    }
                    else if (sort == 2)
                    {
                        sortField = "views";
                    }
                    else
                    {
                        sortField = "PostDate";
                    }
                    if (!string.IsNullOrEmpty(Request["page"]))
                    {
                        if (Request["page"] == "L")
                        {
                            pageIndex = 999;
                        }
                        else
                        {
                            pageIndex = int.Parse(Request["page"].ToString());
                        }
                    }
                    quList = Answers.GetQuestionSearch(pageIndex, 15, Learn, BaseL, sortField, key, st.ToString(), out RecordCount, out anList);
                    int pCount = (int)Math.Ceiling(((decimal)RecordCount) / ((decimal)15));
                    if (pageIndex > pCount) pageIndex = pCount;
                    if (key != "" && Learn != "all" && BaseL != "" && BaseL != "all")
                    {
                        this.DynamicTitle = GetText("AS530", new string[] {key, GetLanguageText(Learn), GetLanguageText(BaseL) });
                        this.DynamicKeywords = GetText("AS531", new string[] {key, GetLanguageText(Learn), GetLanguageText(BaseL) });
                        this.DynamicDescription = GetText("AS532", new string[] { GetLanguageText(Learn), GetLanguageText(BaseL), key });
                    }
                    else
                    {
                        this.DynamicTitle = GetText("AS527", new string[] { key});
                        this.DynamicKeywords = GetText("AS528", new string[] { key });
                        this.DynamicDescription = GetText("AS529", new string[] { key });
                    }

                    if (repQuest != null)
                    {
                        repQuest.DataSource = quList;
                        repQuest.DataBind();
                    }
                    if (repTags != null)
                    {
                        repTags.DataSource = anList;
                        repTags.DataBind();
                    }
                    if (rptLL != null)
                    {
                        List<BLF.LanguageObj> lanObjList = new List<BLF.LanguageObj>();
                        lanObjList.AddRange(BLF.StaticData.GetPopLanguageList);
                        rptLL.DataSource = lanObjList;
                        rptLL.DataBind();
                    }
                    base.OnLoad(e);
                }
            }
            else
            {
                show = "1";
            }
        }
        
        

    }
}


